package com.heihx.mall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heihx.common.utils.PageUtils;
import com.heihx.common.utils.Query;

import com.heihx.mall.product.dao.CategoryDao;
import com.heihx.mall.product.entity.CategoryEntity;
import com.heihx.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //一级分类
        List<CategoryEntity> level1 = categoryEntities.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == 0;
        }).map((menu)->{
            menu.setChildren(getChildren(menu,categoryEntities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null? 0:menu1.getSort()) - (menu2.getSort()==null? 0:menu2.getSort());
        }).collect(Collectors.toList());
        return level1;

    }

    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
    	List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
    		return categoryEntity.getParentCid() == root.getCatId();
    	}).map(categoryEntity -> {
    		categoryEntity.setChildren(getChildren(categoryEntity,all));
    		return categoryEntity;
    	}).sorted((menu1,menu2)->{
            return (menu1.getSort()==null? 0:menu1.getSort()) - (menu2.getSort()==null? 0:menu2.getSort());
        }).collect(Collectors.toList());
    	return children;
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        //新建一个ArrayList集合 接受Long
        List<Long> paths = new ArrayList<>();

        //调用方法找到完整路径[225，22，2]
        List<Long> parentPath = findParentPath(catelogId, paths);

        //调用逆序方法获得正确顺序[2,25,225]
        Collections.reverse(parentPath);


        return parentPath.toArray(new Long[parentPath.size()]);
    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        //1、收集当前节点id
        paths.add(catelogId);
        //2 根据当前节点ID拿到其对应的实体类
        CategoryEntity byId = this.getById(catelogId);
        //3 当实体类父id不为0继续调用这个方法
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        //4 跳出递归循环返回数据
        return paths;

    }
}